home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Applications / Argus TE 2.0 / Argus Libraries 2.0 / FnTE.cp < prev   
Encoding:
Text File  |  1995-06-29  |  17.9 KB  |  636 lines  |  [TEXT/KAHL]

  1. /**********************************************************************
  2.  
  3.     FnTE.cp
  4.  
  5. ***********************************************************************/
  6.  
  7. /*
  8.     Displaying text should be easier than it is.  This library
  9.     provides a group of functions which allow the basics of displaying
  10.     and editing of text in the form of a TE Record.
  11.     
  12.     Functions Include:
  13.     
  14.       FnTE_SetUpTEWindow      Creates TE Record to fill window
  15.       FnTE_MaintainCursor     Call this and TEIdle() in event loop
  16.       FnTE_UpdateWindow       Redraws window contents
  17.       FnTE_DoActivate         Activates/Deactivates window
  18.       FnTE_DoKeyDown          Handles keyboard inputs
  19.       FnTE_DoEditMenu         Handles cut and paste of TE data
  20.       FnTE_DoContent          Handles mousedown in window
  21.       FnTE_DetSBarIncr        Refreshes scroll bar parameters
  22.       FnTE_GrowWindow         Handles resizing of window
  23.       FnTE_FrameRect          Puts frame around text in window
  24.       FnTE_GetTEXT            Puts TEXT resource into TE Record
  25.       
  26.       // Semi-private
  27.       FnTE_AdjustText
  28.       FnTE_SetToCursorPos
  29.       FnTE_ScrollProc
  30. */
  31.  
  32. #define FnCUT        1
  33. #define FnCOPY       2
  34. #define FnPASTE      3
  35. #define FnCLEAR      4
  36. #define FnSELECT_ALL 5
  37. #define DEL_KEY      8
  38. #define MAX_CHAR     32000
  39.  
  40. TEHandle      *tempTEHptr;
  41. ControlHandle *tempVScrollptr;
  42. Cursor        IBeamCursor;
  43. Boolean       IBeamCursorDefined = FALSE;
  44.  
  45. // Prototypes
  46.  
  47. Boolean FnTE_SetUpTEWindow  ( WindowPtr     w,
  48.                               Boolean       hasGrowIcon,
  49.                               Boolean       wordWrap,
  50.                               short         font,
  51.                               short         textSize,
  52.                               short         textInsetPixels,
  53.                               // results //
  54.                               TEHandle      *te,
  55.                               ControlHandle *vScroll );
  56. void    FnTE_MaintainCursor ( WindowPtr     w,
  57.                               TEHandle      *te );
  58. void    FnTE_UpdateWindow   ( WindowPtr     w,
  59.                               TEHandle      *te,
  60.                               Boolean       hasGrowIcon );
  61. void    FnTE_DoActivate     ( WindowPtr     w,
  62.                               TEHandle      *te,
  63.                               ControlHandle *vScroll,
  64.                               Boolean       hasGrowIcon,
  65.                               Boolean       activate );
  66. void    FnTE_DoKeyDown      ( char          theChar,
  67.                               TEHandle      *te,
  68.                               ControlHandle *vScroll,
  69.                               char          *dirty );
  70. void    FnTE_DoEditMenu     ( int           item,
  71.                               TEHandle      *te,
  72.                               ControlHandle *vScroll,
  73.                               char          *dirty,
  74.                               int           cutCommand,
  75.                               int           copyCommand,
  76.                               int           pasteCommand,
  77.                               int           clearCommand,
  78.                               int           selectAllCommand );
  79. void    FnTE_DoContent      ( WindowPtr     w, 
  80.                               EventRecord   *e,
  81.                               TEHandle      *te,
  82.                               ControlHandle *vScroll );
  83. void    FnTE_DetSBarIncr    ( TEHandle      *te,
  84.                               ControlHandle *vScroll );
  85. void    FnTE_GrowWindow     ( WindowPtr     w,
  86.                               TEHandle      *te,
  87.                               ControlHandle *vScroll,
  88.                               short         textInsetPixels,
  89.                               Point         clickLoc );
  90. void   FnTE_FrameRect       ( WindowPtr     w,
  91.                               TEHandle      *te,
  92.                               short         inset );
  93. void   FnTE_GetTEXT         ( short         rsrcID,
  94.                               TEHandle      *te,
  95.                               ControlHandle *vScroll );
  96.  
  97. void    FnTE_AdjustText     ( TEHandle *te, ControlHandle *vScroll );
  98. void    FnTE_SetToCursorPos ( TEHandle *te, ControlHandle *vScroll );
  99. pascal void FnTE_ScrollProc ( ControlHandle theControl,
  100.                               short         theCode );
  101.  
  102. extern void FnErr_DisplayStr(   Str255 s1,
  103.                          Str255 s2,
  104.                          Str255 s3,
  105.                          Str255 s4,
  106.                          int    quitFlag );
  107.  
  108.  
  109. /********** SetUpTEWindow */
  110.  
  111. Boolean FnTE_SetUpTEWindow(
  112.     WindowPtr  w,
  113.     Boolean    hasGrowIcon,
  114.     Boolean    wordWrap,
  115.     short      font,
  116.     short      textSize,
  117.     short      textInsetPixels,
  118.     // results //
  119.     TEHandle      *te,
  120.     ControlHandle *vScroll )
  121. /*
  122.     FnSetUpWindow takes information you supply about a window
  123.     and it's default text parameters then returns a TEHandle to
  124.     a text pane that fills the window and a ControlHandle to
  125.     a scroll bar that is located at the right of the window.
  126.     Returns FALSE if error detected during routine.
  127. */
  128. {
  129.     Rect       destRect, viewRect;
  130.     Rect       vSBarRect;
  131.     GrafPtr    oldPort;
  132.     int        SBarWidth = 15;
  133.  
  134.     // define basic te stuff
  135.     GetPort( &oldPort );
  136.     SetPort( w );
  137.     TextFont( font );
  138.     TextSize( textSize ); 
  139.  
  140.     // set scroll bar rect       
  141.     vSBarRect = (*w).portRect;
  142.     vSBarRect.left = vSBarRect.right - SBarWidth;
  143.     vSBarRect.right += 1;
  144.     vSBarRect.top -= 1;
  145.     if( hasGrowIcon )
  146.         vSBarRect.bottom -= 14;
  147.     else
  148.         vSBarRect.bottom += 1;
  149.  
  150.     // create new control      
  151.     *vScroll = NewControl( w, &vSBarRect, "\p", 1, 0, 0, 0,
  152.         scrollBarProc, 0L);
  153.     if( *vScroll == NULL )
  154.         return FALSE;
  155.  
  156.     // define te view and dest rects
  157.     viewRect = w->portRect;
  158.     viewRect.right -= SBarWidth;
  159.     if( hasGrowIcon )
  160.         viewRect.bottom -= SBarWidth;
  161.     InsetRect(&viewRect, textInsetPixels, textInsetPixels);
  162.     destRect = viewRect;
  163.  
  164.     // create new te record
  165.     *te = TENew( &destRect, &viewRect );
  166.     if( *te == NULL )
  167.         return FALSE;
  168.     if( wordWrap )
  169.         (***te).crOnly = 1;
  170.     else
  171.         (***te).crOnly = -1;
  172.     
  173.     SetPort( oldPort );
  174.     return TRUE;
  175. }
  176.  
  177.  
  178. /********** MaintainCursor */
  179.  
  180. void FnTE_MaintainCursor( WindowPtr w, TEHandle *te )
  181. /*
  182.     Maintains cursor icon in TE window.  Call this function each time
  183.     through event loop (also call Macintosh function TEIdle to blink
  184.     cursor).
  185. */
  186. {
  187.     Point       pt;
  188.     WindowPeek  wPtr;
  189.     GrafPtr     savePort;
  190.     CursHandle  hCurs;
  191.  
  192.     /*
  193.         Variables hIBeamCursor and IBeamCursorDefined must be defined
  194.         at top of this FnTELibrary.c file.
  195.     */
  196.  
  197.     if( !IBeamCursorDefined )
  198.     {
  199.         hCurs = GetCursor(1);  /* IBeam Cusor ID */
  200.         IBeamCursor = **hCurs;
  201.         IBeamCursorDefined = TRUE;
  202.     }
  203.  
  204.     wPtr = (WindowPeek)FrontWindow();
  205.     if( (WindowPtr)wPtr == w )
  206.     {
  207.         if( wPtr->windowKind >= 0 )
  208.         {
  209.             GetPort( &savePort );
  210.             SetPort( (GrafPtr)wPtr );
  211.             GetMouse( &pt );
  212.             if( PtInRect( pt, &(***te).viewRect ) )
  213.             {
  214.                 SetCursor( &IBeamCursor );
  215.             }
  216.             else
  217.             {
  218.                 SetCursor( &arrow );
  219.             }
  220.             SetPort( savePort );
  221.         }
  222.     }
  223. }
  224.  
  225.  
  226. /********** UpdateWindow */
  227.  
  228. void FnTE_UpdateWindow( WindowPtr w, TEHandle *te, Boolean hasGrowIcon )
  229. /*
  230.     Updates a text window.
  231. */
  232. {
  233.     GrafPtr savePort;
  234.  
  235.     GetPort(&savePort);
  236.     SetPort(w);
  237.  
  238.     BeginUpdate(w);
  239.     EraseRect(&w->portRect);
  240.     DrawControls(w);
  241.     if( hasGrowIcon )
  242.         DrawGrowIcon(w);
  243.     TEUpdate(&w->portRect, *te);
  244.     EndUpdate(w);
  245.  
  246.     SetPort(savePort);
  247. }
  248.  
  249.  
  250. /********** DoActivate */
  251. void FnTE_DoActivate( WindowPtr     w,
  252.                       TEHandle      *te,
  253.                       ControlHandle *vScroll,
  254.                       Boolean       hasGrowIcon,
  255.                       Boolean       activate )
  256. /*
  257.     Handles activateEvt for TE window.
  258. */
  259. {
  260.     if( activate )
  261.     {
  262.         TEActivate( *te );
  263.         ShowControl( *vScroll );
  264.         if( hasGrowIcon ) DrawGrowIcon( w );
  265.         TEFromScrap();
  266.     }
  267.     else
  268.     {
  269.         TEDeactivate( *te );
  270.         HideControl( *vScroll );
  271.         if( hasGrowIcon ) DrawGrowIcon( w );
  272.         ZeroScrap();
  273.         TEToScrap();
  274.     }
  275. }
  276.  
  277.  
  278. /********** DoKeyDown */
  279. void FnTE_DoKeyDown( char          theChar,
  280.                      TEHandle      *te,
  281.                      ControlHandle *vScroll,
  282.                      char          *dirty )
  283. /*
  284.     Handles key press in TE region.  Uses functions FnTE_SetToCursorPos
  285.     and FnTE_AdjustText to postion text and scroll bar.
  286. */
  287. {
  288.     int count = 256;  // save as used in FnIO_Library
  289.  
  290.     if( (((***te).teLength + count) >= MAX_CHAR) &&
  291.         (theChar != DEL_KEY) )
  292.     {
  293.         FnErr_DisplayStr(   
  294.             "\pFile size limits have been exceeded.  ",
  295.             "\pPlease save your work and then try to ",
  296.             "\preduce file size.",
  297.             "\p",
  298.             FALSE );  // don't quit
  299.     }
  300.     else
  301.     {
  302.         TEKey( theChar, *te );
  303.         FnTE_SetToCursorPos( te, vScroll );
  304.         FnTE_AdjustText( te, vScroll );
  305.  
  306.         *dirty = 1;
  307.     }
  308. }
  309.  
  310.  
  311. /********** DoEditMenu */
  312.  
  313. void FnTE_DoEditMenu( int           theItem,
  314.                       TEHandle      *te,
  315.                       ControlHandle *vScroll,
  316.                       char          *dirty,
  317.                       int           cutCommand,
  318.                       int           copyCommand,
  319.                       int           pasteCommand,
  320.                       int           clearCommand,
  321.                       int           selectAll )
  322. /*
  323.     Performs cut and paste function of standard Edit menu items.
  324. */
  325. {
  326.     if     ( theItem == cutCommand )   theItem = FnCUT;
  327.     else if( theItem == copyCommand )  theItem = FnCOPY;
  328.     else if( theItem == pasteCommand ) theItem = FnPASTE;
  329.     else if( theItem == clearCommand ) theItem = FnCLEAR;
  330.     else if( theItem == selectAll )    theItem = FnSELECT_ALL;
  331.  
  332.     switch( theItem )
  333.     {
  334.         case FnCUT:
  335.             TECut( *te );
  336.             FnTE_DetSBarIncr( te, vScroll );
  337.             FnTE_SetToCursorPos( te, vScroll );
  338.             FnTE_AdjustText( te, vScroll );
  339.             *dirty = 1;
  340.             break;
  341.         case FnCOPY:
  342.             TECopy( *te );
  343.             break;
  344.         case FnPASTE:
  345.             if(TEGetScrapLen() + (***te).teLength < MAX_CHAR)
  346.             {
  347.                 TEPaste( *te );
  348.                 FnTE_DetSBarIncr( te, vScroll );
  349.                 FnTE_SetToCursorPos( te, vScroll );
  350.                 FnTE_AdjustText( te, vScroll );
  351.                 *dirty = 1;
  352.             }
  353.             else
  354.             {
  355.                 FnErr_DisplayStr(
  356.                     "\pPaste size too large.","\p","\p","\p", FALSE);
  357.             }
  358.             break;
  359.         case FnCLEAR:
  360.             TEDelete( *te );
  361.             FnTE_DetSBarIncr( te, vScroll );
  362.             FnTE_SetToCursorPos( te, vScroll );
  363.             FnTE_AdjustText( te, vScroll );
  364.             *dirty = 1;
  365.             break;
  366.         case FnSELECT_ALL:
  367.             TESetSelect( 0, 32767, *te );
  368.             FnTE_SetToCursorPos( te, vScroll );
  369.             FnTE_AdjustText( te, vScroll );
  370.             break;
  371.     }
  372. }
  373.  
  374.  
  375. /********** DoContent */
  376.  
  377. void FnTE_DoContent( WindowPtr     w, 
  378.                      EventRecord   *e,
  379.                      TEHandle      *te,
  380.                      ControlHandle *vScroll )
  381. /*
  382.     Use this procedure when user clicks in content region of TE window.
  383.     Uses the TnTE_ScrollProc below to take care of scroll bar function.
  384.     Also uses FnTE_AdjustText function.
  385. */
  386. {
  387.     int                cntlCode;
  388.     ControlHandle      cntl;
  389.     int                pageSize;
  390.     GrafPtr            savePort;
  391.  
  392.     GetPort(&savePort);
  393.     SetPort(w);
  394.  
  395.     GlobalToLocal(&e->where);
  396.     if( (cntlCode = FindControl(e->where, w, &cntl)) == 0 )
  397.     {
  398.         if( PtInRect( e->where, &(***te).viewRect) )
  399.             TEClick( e->where, (e->modifiers & shiftKey) != 0, *te );
  400.     }
  401.     else if( cntlCode == inThumb )
  402.     {
  403.         TrackControl( cntl, e->where, 0L );
  404.         FnTE_AdjustText( te, vScroll );
  405.     }
  406.     else
  407.     {
  408.         tempTEHptr = te;
  409.         tempVScrollptr = vScroll;
  410.         TrackControl( cntl, e->where, FnTE_ScrollProc );
  411.     }
  412.  
  413.     SetPort(savePort);
  414. }
  415.  
  416.  
  417. /********** DetSBarIncr */
  418.  
  419. void FnTE_DetSBarIncr( TEHandle *te, ControlHandle *vScroll )
  420. /*
  421.     Determines number of scroll bar increments based on amount of
  422.     text pointed to in TEHandle te.
  423. */
  424. {
  425.     register int nIncr, nLinesDisplayed, len;
  426.  
  427.     nLinesDisplayed = ((***te).viewRect.bottom-(***te).viewRect.top) / 
  428.         (***te).lineHeight;
  429.     nIncr = (***te).nLines - nLinesDisplayed;
  430.     len = (***te).teLength;
  431.     if( len <= 1 ) len = 1;
  432.     if( (*((***te).hText))[len-1] == '\r' )
  433.       nIncr++;
  434.  
  435.     if( nIncr < 0 )
  436.         nIncr = 0;
  437.     SetCtlMax( *vScroll, nIncr );
  438. }
  439.  
  440.  
  441. /********** GrowWindow */
  442.  
  443. void FnTE_GrowWindow( WindowPtr     w,
  444.                       TEHandle      *te,
  445.                       ControlHandle *vScroll,
  446.                       short         textInsetPixels,
  447.                       Point         clickLoc )
  448. /*
  449.     Use this function to resize a TE window.
  450. */
  451. {
  452.     GrafPtr savePort;
  453.     long    theResult;
  454.     Rect    oldHorizBar;
  455.     Rect    r;
  456.     int     SBarWidth = 15;
  457.  
  458.     GetPort( &savePort );
  459.     SetPort( w );
  460.     oldHorizBar = w->portRect;
  461.     oldHorizBar.top = oldHorizBar.bottom - (SBarWidth + 1);
  462.     SetRect( &r,
  463.              100,
  464.              100,
  465.              screenBits.bounds.right,
  466.              screenBits.bounds.bottom );
  467.     theResult = GrowWindow( w, clickLoc, &r );
  468.     if (theResult == 0)
  469.       return;
  470.     SizeWindow( w, LoWord(theResult), HiWord(theResult), FALSE );
  471.     // InvalRect( &w->portRect ); don't need, window redrawn here
  472.     (***te).viewRect = w->portRect;
  473.     (***te).viewRect.right -= SBarWidth;
  474.     (***te).viewRect.bottom -= SBarWidth;
  475.     InsetRect( &(***te).viewRect, textInsetPixels, textInsetPixels );
  476.     (***te).destRect = (***te).viewRect;
  477.     TECalText( *te );
  478.     //EraseRect( &oldHorizBar ); erase entire window instead
  479.     EraseRect( &w->portRect );
  480.     MoveControl( *vScroll,
  481.                  w->portRect.right - SBarWidth,
  482.                  w->portRect.top - 1 );
  483.     SizeControl( *vScroll,
  484.                  SBarWidth + 1,
  485.                  w->portRect.bottom - w->portRect.top - (SBarWidth-2) );
  486.     // r = (***vScroll).contrlRect; use with next
  487.     // ValidRect( &r ); only need if invalidated window above
  488.     FnTE_DetSBarIncr( te, vScroll );
  489.     FnTE_AdjustText( te, vScroll );
  490.     DrawGrowIcon(w);  // has to have grow icon
  491.     TEUpdate(&w->portRect, *te);
  492.  
  493.     SetPort( savePort );
  494. }
  495.  
  496.  
  497. /********** FrameRect */
  498.  
  499. void FnTE_FrameRect( WindowPtr w, TEHandle *te, short inset )
  500. {
  501.     Rect     itemRect;
  502.     PenState thePnState;
  503.     GrafPtr  oldPort;
  504.  
  505.     GetPort( &oldPort );
  506.     SetPort( w );
  507.     itemRect = (***te).viewRect;
  508.     InsetRect( &itemRect, -inset, -inset );
  509.     itemRect.right += 1;
  510.     GetPenState( &thePnState );
  511.     PenNormal();
  512.     FrameRect( &itemRect );
  513.     SetPenState( &thePnState );
  514.     SetPort( oldPort );
  515. }
  516.  
  517.  
  518. /********** GetTEXT */
  519.  
  520. void FnTE_GetTEXT( short rsrcID, TEHandle *te, ControlHandle *vScroll )
  521. /*
  522.     Reads in TEXT resource into TE record and resets scroll bar.  Need
  523.     to call FnTE_UpdateWindow to display text.
  524. */
  525. {
  526.     (**te)->hText = GetResource( 'TEXT', rsrcID );
  527.     TECalText( *te );
  528.     (***te).destRect.top = (***te).viewRect.top;
  529.     SetCtlValue( *vScroll, GetCtlMin( *vScroll ) );
  530.     FnTE_DetSBarIncr( te, vScroll );
  531. }
  532.  
  533.  
  534. /********** AdjustText */
  535.  
  536. void FnTE_AdjustText( TEHandle *te, ControlHandle *vScroll )
  537. /*
  538.     Takes current value of the scroll bar position and adjusts text
  539.     for specified TextEdit pane.  Call this function after user
  540.     clicks in scroll bar.
  541. */
  542. {
  543.     int  oldScroll, newScroll, delta;
  544.  
  545.     oldScroll = (***te).viewRect.top - (***te).destRect.top;
  546.     newScroll = GetCtlValue( *vScroll ) * (***te).lineHeight;
  547.     delta = oldScroll - newScroll;
  548.     if( delta != 0 )
  549.         TEScroll( 0, delta, *te );
  550. }
  551.  
  552.  
  553. /********** SetToCursorPos */
  554.  
  555. void FnTE_SetToCursorPos( TEHandle *te, ControlHandle *vScroll )
  556. /*
  557.     Use this function to adjust scroll bar position such that the
  558.     currently selected text and/or insertion cursor position is 
  559.     displayed in middle of te window.  You must make a call to a 
  560.     function like FnTE_AdjustText after calling this function to redraw
  561.     text based on new scroll bar position.
  562. */
  563. {
  564.     register int topLine,bottomLine,theLine,nIncr,nLinesDisplayed,len;
  565.  
  566.     // following could be replaced by call to FnDetSBarIncr
  567.     nLinesDisplayed = ((***te).viewRect.bottom-(***te).viewRect.top) / 
  568.         (***te).lineHeight;
  569.     nIncr = (***te).nLines - nLinesDisplayed;
  570.     
  571.     len = (***te).teLength;
  572.     if( len <= 1 ) len = 1;
  573.     if( (*((***te).hText))[len-1] == '\r' )
  574.       nIncr++;
  575.  
  576.     if( nIncr < 0 )
  577.         nIncr = 0;
  578.     SetCtlMax( *vScroll, nIncr );
  579.  
  580.     topLine = GetCtlValue( *vScroll );
  581.     bottomLine = topLine + nLinesDisplayed;
  582.     if( (***te).selStart < (***te).lineStarts[topLine] ||
  583.         (***te).selStart >= (***te).lineStarts[bottomLine] )
  584.     {
  585.         for( theLine = 0;
  586.             (***te).selStart >= (***te).lineStarts[theLine];
  587.             theLine++ )
  588.             /* count */ ;
  589.         SetCtlValue( *vScroll, theLine - nLinesDisplayed / 2 );
  590.     }
  591. }
  592.  
  593.  
  594. /********** PASCAL ScrollProc */
  595.  
  596. pascal void FnTE_ScrollProc( ControlHandle theControl, short theCode )
  597. /*
  598.     Scroll procedure used for TE scroll bar.  Uses FnTE_AdjustText
  599.     function to update text.
  600. */
  601. {
  602.     int    pageSize;
  603.     int    scrollAmt;
  604.     int    oldCtl;
  605.  
  606.     if( theCode == 0 )
  607.         return;
  608.  
  609.     pageSize =
  610.         ((***tempTEHptr).viewRect.bottom -
  611.         (***tempTEHptr).viewRect.top) /
  612.         (***tempTEHptr).lineHeight - 1;
  613.  
  614.     switch( theCode )
  615.     {
  616.         case inUpButton: 
  617.             scrollAmt = -1;
  618.             break;
  619.         case inDownButton: 
  620.             scrollAmt = 1;
  621.             break;
  622.         case inPageUp: 
  623.             scrollAmt = -pageSize;
  624.             break;
  625.         case inPageDown: 
  626.             scrollAmt = pageSize;
  627.             break;
  628.     }
  629.  
  630.     oldCtl = GetCtlValue( theControl );
  631.     SetCtlValue( theControl, oldCtl + scrollAmt );
  632.  
  633.     FnTE_AdjustText( tempTEHptr, tempVScrollptr );
  634. }
  635.  
  636. // End of File